package com.xuecheng.media.controller;

import com.xuecheng.api.media.QueryMediaModel;
import com.xuecheng.api.media.model.MediaDTO;
import com.xuecheng.api.media.model.vo.MediaAuditVO;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.entity.Media;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController {

    @Autowired
    private MediaService  mediaService;
    //预览媒资
    @GetMapping("/media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {

        Long companyId = SecurityUtil.getCompanyId();

        String vodUrl = mediaService.getVODUrl(mediaId,companyId);

        return vodUrl;
    }
    //添加媒资
    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {
        Media media = new Media();
        BeanUtils.copyProperties(vo,media);
        MediaDTO mediaDTO = MediaConvert.INSTANCE.entity2dto(media);
        MediaDTO dto = mediaService.createMedia(mediaDTO);
        return dto;
    }
    //查询媒资
    @PostMapping("media/list")
    public PageVO<MediaDTO> queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel media) {
        Long companyId = SecurityUtil.getCompanyId();

        PageVO<MediaDTO> page=mediaService.queryMediaList(params,media,companyId);
        return page;
    }
    //删除媒资
    @DeleteMapping("media/{mediaId}")
    public boolean deleteMedia(@PathVariable Long mediaId){
        boolean delete = mediaService.deleteMedia(mediaId);
        return delete;
    }


    //平台查询
    @PostMapping("/m/media/list")
    public PageVO<MediaDTO> queryMediaForOperation(PageRequestParams pageRequestParams, @RequestBody QueryMediaModel mediaQueryDTO) {
        Long companyId = SecurityUtil.getCompanyId();

        return mediaService.queryMediaList(pageRequestParams, mediaQueryDTO,companyId);
    }
    //平台预览媒资
    @GetMapping("m/media/preview/{mediaId}")
    public String previewMedia1(@PathVariable Long mediaId) {

        Long companyId = SecurityUtil.getCompanyId();

        String vodUrl = mediaService.getVODUrl(mediaId,companyId);

        return vodUrl;
    }
    //平台审核
    @PutMapping("/m/media/audit")
    public boolean auditMedia(@RequestBody MediaAuditVO mediaAuditVO) {
        MediaDTO mediaDTO = MediaConvert.INSTANCE.fromAuditVO(mediaAuditVO);
        mediaDTO.setAuditMind(mediaDTO.getAuditMind());
        return mediaService.auditMedia(mediaDTO);
    }
}
